package demo4.mapper;

import demo4.pojo.User;
import demo4.util.IDBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Mapper是数据持久层，作用就是和数据库打交道，通过基本的增删改查操作
 * 该类就是和数据库中的user表进行操作
 */
@Component
public class UserMapper implements IUserMapper{
    @Autowired
    private IDBUtil dbUtil;
    public int insert(User user){
        //System.out.println("UserMapper：插入用户数据：");
        //要插入数据库，那么要先连接数据库
        try(Connection connection = dbUtil.getConnection();) {
            String sql = "insert into user(username,password,nickname,age) values(?,?,?,?)";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setString(3,user.getNickname());
            ps.setInt(4,user.getAge());
            return ps.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public User selectByUsername(String username) {
        try (Connection connection = dbUtil.getConnection();){
            String sql = "select id,username,password,nickname,age " +
                    "from user " +
                    "where username=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs =ps.executeQuery();
            if(rs.next()){//如果为true，则说明用户存在
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int update(User user) {
        try(Connection connection =dbUtil.getConnection();) {
            String sql = "update user set password=?,nickname=?,age=? where username=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,user.getPassword());
            ps.setString(2,user.getNickname());
            ps.setInt(3,user.getAge());
            ps.setString(4,user.getUsername());
            return ps.executeUpdate();
        } catch (SQLException e) {
           e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<User> selectAll() {
        List<User> list = new ArrayList<>();
        try (Connection connection = dbUtil.getConnection()) {
            String sql = "select id,username,password,nickname,age from user";
            PreparedStatement ps = connection.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
                while(rs.next()){
                    User user = new User();
                    user.setId(rs.getInt("id"));
                    user.setUsername(rs.getString("username"));
                    user.setPassword(rs.getString("password"));
                    user.setNickname(rs.getString("nickname"));
                    user.setAge(rs.getInt("age"));
                    list.add(user);
                }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int deleteByUsername(String username) {
        try (Connection connection = dbUtil.getConnection();){
            String sql = "delete from user where username=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,username);
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
